package com.ewing.busi.api.login.action;

import java.io.File;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.util.ResourceUtils;

import com.ewing.busi.api.login.dto.SmsAuthorizeReq;
import com.ewing.busi.api.login.service.SmsAuthorizeService;
import com.ewing.common.constants.ResponseType;
import com.ewing.common.exception.BusinessException;
import com.ewing.common.utils.SystemPropertyUtils;
import com.ewing.core.app.action.base.BaseAction;
import com.ewing.core.app.action.base.ResponseData;
import com.ewing.core.auth.HttpSessionUtils;
import com.ewing.utils.JsonUtils;

/**
 * 短信验证登陆的接口
 * 
 * @author Administrator
 */
public class SmsAuthorizeAction extends BaseAction {

  private static final long serialVersionUID = 1L;

  private static Logger logger = Logger.getLogger(SmsAuthorizeAction.class);

  @Resource
  private SmsAuthorizeService smsAuthorizeService;

  /**
   * 是否客户，如果之前未登陆过，就需要注册，否则进行登陆
   */
  public void isCustomer() {
    try {
      SmsAuthorizeReq req = getParamJson(SmsAuthorizeReq.class);
      checkRequired(req, "req不能为空");

      boolean result = smsAuthorizeService.isCustomer(req);

      String file =
          FileUtils.readFileToString(new File(ResourceUtils
              .getURL(
                  result ? "classpath:../html/auth/login.html"
                      : "classpath:../html/auth/register.html").getFile()));
      outSucResult(file);
    } catch (BusinessException e) {
      logger.error(e.getMessage(), e);
      outAlert(e.getMessage());
    } catch (Exception e) {
      HttpSessionUtils.getRedirectUrl(true);
      logger.error(e.getMessage(), e);
    }
  }

  /**
   * 发送验证码
   */
  public void sendAuthCode() {
    try {
      SmsAuthorizeReq req = getParamJson(SmsAuthorizeReq.class);
      checkRequired(req, "req不能为空");

      boolean result = smsAuthorizeService.sendAuthCode(req);
      outSucResult(result);
    } catch (BusinessException e) {
      logger.error(e.getMessage(), e);
      outAlert(e.getMessage());
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
  }

  /**
   * 短信验证码登陆<br/>
   * 提供客户验证码登陆功能
   * 
   * @author Joeson
   */
  public void authBySms() {
    boolean result = false;
    String redirectUrl = null;
    try {
      SmsAuthorizeReq req = getParamJson(SmsAuthorizeReq.class);
      checkRequired(req, "req不能为空");

      logger.info(JsonUtils.toJson(req));

      result = smsAuthorizeService.authCode(req);
      redirectUrl = HttpSessionUtils.getRedirectUrl(true);
    } catch(BusinessException e){
      logger.error(e.getMessage(), e);
      outJsonResult(ResponseData.initResponseData(true, request.getCookies(), e.getMessage(), ResponseType.ALERT));
    }catch (Exception e) {
      logger.error(e.getMessage(), e);
    } finally {
      HttpSessionUtils.getRedirectUrl(true);// 删除redirecturl
    }

    logger.info("redirectUrl : " + redirectUrl);
    redirectUrl =
        StringUtils.isEmpty(redirectUrl) ? SystemPropertyUtils.getShopDomain()
            + "/module/index.html?shopId" + getShopId() : redirectUrl;
    if (result) {
      // 进行重定向
      outJsonResult(ResponseData.initResponseData(true, request.getCookies(), redirectUrl,
          ResponseType.REDIRECT));
    } else {
      outFailResult("登陆验证失败");
    }
  }

}
